from typing import List, Any
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session

from ....api import deps
from ....schemas.student import StudentResponse, StudentUpdate
from ....models.user import User
from ....models.student import Student
from ....crud.student import student_crud

router = APIRouter()

@router.get("/", response_model=List[StudentResponse])
def read_students(
        skip: int = 0,
        limit: int = 100,
        db: Session = Depends(deps.get_db),
        current_user: User = Depends(deps.get_current_user_with_permissions(["admin", "counselor"]))
) -> Any:
    """
    获取学生列表（仅限管理员和辅导员访问）
    """
    students = student_crud.get_multi(db, skip=skip, limit=limit)
    return students

@router.get("/me", response_model=StudentResponse)
def read_student_me(
        db: Session = Depends(deps.get_db),
        current_user: User = Depends(deps.get_current_user)
) -> Any:
    """
    获取当前登录学生信息
    """
    if current_user.role != "student":
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="只有学生可以访问此接口"
        )

    student = student_crud.get_by_user_id(db, user_id=current_user.id)
    if not student:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="学生信息不存在"
        )
    return student

@router.get("/{account_id}", response_model=StudentResponse)
def read_student(
        student_id: int,
        db: Session = Depends(deps.get_db),
        current_user: User = Depends(deps.get_current_user_with_permissions(["admin", "counselor"]))
) -> Any:
    """
    通过ID获取学生信息（仅限管理员和辅导员访问）
    """
    student = student_crud.get(db, id=student_id)
    if not student:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="学生不存在"
        )
    return student

@router.put("/me", response_model=StudentResponse)
def update_student_me(
        student_in: StudentUpdate,
        db: Session = Depends(deps.get_db),
        current_user: User = Depends(deps.get_current_user)
) -> Any:
    """
    更新当前登录学生信息
    """
    if current_user.role != "student":
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="只有学生可以更新学生信息"
        )

    student = student_crud.get_by_user_id(db, user_id=current_user.id)
    if not student:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="学生信息不存在"
        )

    student = student_crud.update(db, db_obj=student, obj_in=student_in)
    return student

@router.get("/by-class/{class_name}", response_model=List[StudentResponse])
def get_students_by_class(
        class_name: str,
        db: Session = Depends(deps.get_db),
        current_user: User = Depends(deps.get_current_user_with_permissions(["admin", "counselor"]))
) -> Any:
    """
    根据班级获取学生列表（仅限管理员和辅导员访问）
    """
    students = db.query(Student).filter(Student.class_ == class_name).all()
    return students

@router.get("/by-major/{major_name}", response_model=List[StudentResponse])
def get_students_by_major(
        major_name: str,
        db: Session = Depends(deps.get_db),
        current_user: User = Depends(deps.get_current_user_with_permissions(["admin", "counselor"]))
) -> Any:
    """
    根据��业获取学生列表（仅限管理员和辅导员访问）
    """
    students = db.query(Student).filter(Student.major == major_name).all()
    return students

@router.get("/by-academy/{academy_name}", response_model=List[StudentResponse])
def get_students_by_academy(
        academy_name: str,
        db: Session = Depends(deps.get_db),
        current_user: User = Depends(deps.get_current_user_with_permissions(["admin", "counselor"]))
) -> Any:
    """
    根据学院获取学生列表（仅限管理员和辅导员访问）
    """
    students = db.query(Student).filter(Student.academy == academy_name).all()
    return students